//
//  MyShowViewController.m
//  QXShowing
//
//  Created by 王道道 on 2018/4/20.
//  Copyright © 2018年 doll_dream. All rights reserved.
//

#import "MyShowViewController.h"

#import "ProductListViewController.h"
//#import "PictureManageViewController.h"
#import "NewPictureManageViewController.h"
#import "MyShowTableViewCell.h"
#import "NewApplyShowViewController.h"
#import "EditCompanyInfoViewController.h"

#import "MyShowListModel.h"

static NSString *const listTextCell = @"listTextCell";
static NSString *const listCell = @"listCell";

@interface MyShowViewController ()<UITableViewDelegate,UITableViewDataSource,TabelViewRefreshDelegate>
@property (nonatomic, strong) RefreshTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MyShowViewController

#pragma mark - Lazy
- (RefreshTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[RefreshTableView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, App_Frame_Height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.isRefresh = YES;
        _tableView.refreshDelegate = self;
        _tableView.tableHeaderView = [[UIView alloc]init];
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.scrollsToTop = NO;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"展位编辑";
    [self initNavBarBackBtn];
    
    [self initRightBtn];
    
    [self.tableView headerRefresh];
}

- (void)initRightBtn {
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 60, 44)];
    [rightButton setImage:[UIImage imageNamed:@"my-2"] forState:UIControlStateNormal];
//    [rightButton setTitle:title forState:UIControlStateNormal];
//    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightButton setBackgroundColor:[UIColor clearColor]];
    [rightButton addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)addAction {
    NewApplyShowViewController *next = [[NewApplyShowViewController alloc] init];
    [self pushViewController:next animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, App_Frame_Width, CGRectGetHeight(self.view.bounds));
}


#pragma mark - TabelViewRefreshDelegate
- (void)tableView:(UITableView *)tableView Refresh:(BOOL)refresh PageCount:(NSInteger)pageCount {
    [self requestPostBH_User_getUserBoothList:refresh PageCount:pageCount];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    } else {
        return self.dataArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listCell];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:listCell];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if (indexPath.row == 0) {
            cell.textLabel.text = @"公司产品管理";
        } else if (indexPath.row == 1) {
            cell.textLabel.text = @"公司轮播管理";
        } else {
            cell.textLabel.text = @"公司介绍管理";
        }
        cell.textLabel.font = BHFont_Size_15;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    } else {
        MyShowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listTextCell];
        if (cell == nil) {
            cell = [[MyShowTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:listCell];
        }
        cell.model = self.dataArray[indexPath.row];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 60.0;
    } else {
        return 65.0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.0;
    }
    else {
        return 45.0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UIView *view = [[UIView alloc] init];
        return view;
    }
    else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, 45.0)];
        view.backgroundColor = Bg_Color_FFFFFF;
        
        UIImageView *headerIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon-34-26"]];
        [view addSubview:headerIV];
        [headerIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(view.mas_centerX).offset(-50);
//            make.width.height.offset(15);
            make.width.offset(17);
            make.height.offset(13);
        }];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = @"我的展位";
        label.font = BHFont_Size_15;
        label.textColor = Text_Color_000000;
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(headerIV.mas_right).offset(15);
        }];
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 44.5, App_Frame_Width, 0.5f)];
        line.backgroundColor = [UIColor lightGrayColor];
        [view addSubview:line
         ];
        
        return view;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 8.0;
    }
    else {
        return 0.0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, 8.0)];
        view.backgroundColor = Bg_Color_F7F7F7;
        return view;
    }
    else {
        UIView *view = [[UIView alloc] init];
        return view;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            ProductListViewController *next = [[ProductListViewController alloc] init];
            [self pushViewController:next animated:YES];
        } else if (indexPath.row == 1) {
            NewPictureManageViewController *next = [[NewPictureManageViewController alloc] init];
            [self pushViewController:next animated:YES];
        } else {
            EditCompanyInfoViewController *next = [[EditCompanyInfoViewController alloc] init];
            [self pushViewController:next animated:YES];
        }
    } else {
        
    }
}

#pragma mark - 接口
- (void)_loadData:(id)response Refresh:(BOOL)refresh {
    if (refresh) [self.dataArray removeAllObjects];
    NSArray *arr = [NSArray yy_modelArrayWithClass:[MyShowListModel class] json:response];
    [self.dataArray addObjectsFromArray:arr];
    if (arr.count < 10) {
        [self.tableView endRefreshingWithNoMoreData];
    }
    [self.tableView reloadData];
}

- (void)requestPostBH_User_getUserBoothList:(BOOL)refresh PageCount:(NSInteger)pageCount {
    SGUserModel *userModel = [SGUserInfo getUserInfo];
//    self.dataArray.count != 0 ?: [[BHAlertUtil alertManager]showLoadingView:self.view];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:@"" forKey:@"uid"];
//    [params setObject:@"14" forKey:@"company_id"];
    if (![BHSettingUtil dataAndStringIsNull:userModel.company_id]) {
        [params setObject:userModel.company_id forKey:@"company_id"];
    }
    else if (![BHSettingUtil dataAndStringIsNull:userModel.Id]) {
        [params setObject:userModel.Id forKey:@"uid"];
        }
    else {

    }
    
    [params setObject:[NSString stringWithFormat:@"%ld",(long)pageCount] forKey:@"page"];
    [params setObject:@"10" forKey:@"pagesize"];
    
    [PPHTTPRequest requestPostBH_User_getUserBoothList:^(id response, BOOL success) {
        if (self.dataArray.count == 0 && [response isKindOfClass:[NSString class]]) {
            //            self.collectionView.placeholderShow(YES).prompt_title((NSString *)response).isButtonHidden(YES);
        }else self.tableView.placeholderShow(NO);
        if (success && ![response isKindOfClass:[NSString class]]) {
            [self _loadData:response Refresh:refresh];
        }
        if (success && refresh == YES) {
            [self _loadData:response Refresh:refresh];
        }
        [[BHAlertUtil alertManager]hiddenLoading];
    } params:params];
}

@end
